<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
global $data, $admin;

/**
 * Description of empresas
 *
 * @author Golem
 */
class Empresas extends CI_Controller {

    public function __construct() {
        parent::__construct();
        global $admin, $data;
        # cargamos variables
        $admin = $this->session->userdata('admin');
        $admin['place'] = 'empresas';
        # verifica si pasó por sushitto
        if ($admin['ta_tipo_administrador_id'] != 1) {
            redirect('home');
        }
        $this->output->enable_profiler(FALSE);
    }

    public function index() {
        global $data, $admin;
        $data['empresa'] = $this->cc_model_a->lista_empresas();
        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_admin', $data);
        $this->load->view('empresas/content');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function alta() {
        global $data, $admin;
        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_admin', $data);
        $this->load->view('empresas/alta');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function save() {
        $this->form_validation->set_rules('e_nombre', 'e_nombre', 'required|trim|xss_clean|min_length[2]|max_length[30]');

        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            redirect('empresas');
        }
        #configuraciones de carga
        $config['upload_path'] = './assets/img/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '5000';
        $config['max_width'] = '2000';
        $config['max_height'] = '2000';
        $config['overwrite'] = FALSE;
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = TRUE;

        #carga
        $this->load->library('upload', $config);
        #si la carga falla regresamos
        if (!$this->upload->do_upload('myfile')) {
            $errorMsj = '   <div class="alert alert-error" style="margin-top: 16px;">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>Error al cargar el documento </strong> ' . $this->upload->display_errors() . '</div>';
            $this->session->set_userdata('mensaje', $errorMsj);
            redirect(base_url('empresas/alta'));
        }
        #carga de documento correcta, recuperamos información
        $carga = $this->upload->data();
        $e_nombre = $this->security->xss_clean($this->input->post('e_nombre'));
        $this->cc_model_a->gEmpresa($e_nombre, $carga['file_name']);
        redirect('empresas');
    }

}
